package 比屋作业.day19;
import java.util.*;
public class D19_2 {
    public static void main(String[] args) {
    MovieRatingSysterm systerm=new MovieRatingSysterm();
    systerm.addRatingToMovie("星球大战",4.5);
    systerm.addCommentToMovie("星球大战","科幻经典");
    systerm.addCommentToMovie("复仇者联盟","超级英雄集结，非常震撼！");
    systerm.printMovieDetails("星球大战");
    System.out.println();
    systerm.printMovieDetails("复仇者联盟");
    }
}
class Movie{
   private String name;
   private List<Double> ratings;
   private List<String> comments;
   public Movie(String name){
       this.name=name;
       this.ratings=new ArrayList<>();
       this.comments=new ArrayList<>();
   }
   public void addRating(double rating){
       ratings.add(rating);
   }
   public void addComment(String comment){
       comments.add(comment);
   }
   public double getAverageRating(){
       if (ratings.isEmpty()){
           return 0.0;
       }
       double sum=0;
       for (double rating:ratings){
           sum+=rating;
       }
       return sum/ratings.size();
   }
    public String getName() {
        return name;
    }
    public List<String>getComments(){
       return comments;
    }
}
class MovieRatingSysterm{
    private Map<String,Movie>movies;
    public MovieRatingSysterm(){
        movies=new HashMap<>();
    }
    public void addRatingToMovie(String movieName,double rating){
        Movie movie=movies.getOrDefault(movieName,new Movie(movieName));
        movie.addRating(rating);
        movies.put(movieName,movie);
    }
    public void addCommentToMovie(String movieName,String comment){
        Movie movie=movies.getOrDefault(movieName,new Movie(movieName));
        movie.addComment(comment);
        movies.put(movieName,movie);
    }
    public void printMovieDetails(String movieName) {
        Movie movie = movies.get(movieName);
        if (movieName == null) {
            System.out.println("电影：" + movieName + "不存在！");
            return;
        }
        System.out.println("电影"+movie.getName());
        System.out.println("平均评分："+movie.getAverageRating());
        System.out.println("评论：");
        for (String comment:movie.getComments()){
            System.out.println(comment);
        }
    }
}